package com.g2room.search.es.engine;

import java.util.List;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
public class Index {
	private EsClient esClient;
	public Index(){
		esClient = new EsClient();
	}
	public Index(String index){
		esClient = new EsClient(index);
	}
	/**
	 * 批量删除索引
	 * @param ids
	 * @param type
	 * @return	删除成功返回true，反之返回false，包括只删除一部分也返回false。
	 */
	public boolean delIndexes(List<Long> ids, String type) {
		boolean rs = true;
		for(long id : ids) {
			DeleteResponse dr = esClient.getClient().prepareDelete(esClient.getIndex(), type, String.valueOf(id)).execute().actionGet(); 
			if(rs) rs = dr.isFound();
		}
		return rs;
	}
	
	/**
	 * 单个删除索引
	 * @param id
	 * @param type
	 * @return 删除成功返回true，反之返回false
	 */
	public boolean delIndex(Long id, String type) {
//		AdminClient ac = esClient.getClient().admin();
//		DeleteIndexRequestBuilder drb = ac.indices().prepareDelete(esClient.getIndex(), type, String.valueOf(id));
//		DeleteIndexResponse res = drb.execute().actionGet();
////		DeleteResponse dr = drb.execute().actionGet();
		
		DeleteResponse dResponse = esClient.getClient().prepareDelete(esClient.getIndex(), type, id+"").setRouting("id").execute()
                .actionGet();
		return dResponse.isFound();
	}
	
	// 清除所有索引
    public void deleteIndex() {
        IndicesExistsResponse indicesExistsResponse = esClient.getClient().admin().indices()
                .exists(new IndicesExistsRequest(new String[] {esClient.getIndex()}))
                .actionGet();
        if (indicesExistsResponse.isExists()) {
        	esClient.getClient().admin().indices().delete(new DeleteIndexRequest(esClient.getIndex()))
                    .actionGet();
        }
    }
    
    // 删除Index下的某个Type
    public void deleteType(String type, String id){
    	esClient.getClient().prepareDelete().setIndex(esClient.getIndex()).setType(type).setId(id).execute().actionGet();
    }
	
	/**
	 * 插入数据到ES
	 * @param id	插入的文档ID
	 * @param json	json数据
	 * @param index	
	 * @param type
	 * @return	插入则返回true，暂时未验证更新返回值
	 */
	public boolean createIndex(String id, String json, String type) {
		IndexRequestBuilder irb = esClient.getClient().prepareIndex(esClient.getIndex(), type);
		String routing = EsClient.getRouting(type);
		if(null != routing) irb.setRouting(routing);
		irb.setId(id);
		irb.setSource(json);
//		irb.field("analyzer","ik");
		IndexResponse ir = irb.execute().actionGet();
		return ir.isCreated();
	}
	
	/**
	 * 批量插入数据到ES
	 * @param ids	插入的ID集合
	 * @param jsons	json数据集合
	 * @param index
	 * @param type
	 * @return
	 */
	public boolean createIndexBatch(List<String> ids, List<String> jsons, String type) {
		boolean rs = false;
		if(null == jsons || jsons.isEmpty()) return rs;
		
		Client client = esClient.getClient();
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		String routing = EsClient.getRouting(type);
		for(int i =0; i< ids.size(); i++) {
			IndexRequestBuilder irb = client.prepareIndex(esClient.getIndex(), type);
			if(null != routing) irb.setRouting(routing);
			irb.setId(ids.get(i));
			bulkRequest.add(irb.setSource(jsons.get(i)));
		}
		BulkResponse br = bulkRequest.execute().actionGet();
		System.out.println("====="+br.buildFailureMessage());
		rs = br.hasFailures();
		return rs;
	}
}
